import request from '../../../request/index.js'

let getClientId = () => {
    return new Promise((resolve, reject) => {
        request.request({
            url: "/functional-third-party-web/google/getClientId",
            method: "post",
            stateSuccess: (res) => {
                resolve(res);
            },
            stateFail: (error) => {
                reject(error);
            }
        });
    })
}

let login = () => {

    return new Promise((resolve, reject) => {

        getClientId().then((res) => {
            google.accounts.id.initialize({
                client_id: res.data,
                callback: (response) => {
                    console.log(response.credential);
                    // /functional-third-party-web/google
                    request.request({
                        url: "/functional-third-party-web/google/login",
                        method: "post",
                        data: {
                            credential: response.credential,
                        },
                        stateSuccess: (res) => {
                            resolve(res)
                        },
                        stateFail: (error) => {
                            reject(error)
                        }
                    });

                }
            });
            google.accounts.id.prompt((notification) => {
                console.log(notification, 'notification');
                // if (notification.isNotDisplayed() || notification.isSkippedMoment()) {
                // }
            });
        }).catch(error => {

        })

    });
};

export default {
    login
}